'''
Created on Jan 18, 2010

@author: madsk
'''

from PyQt4 import QtCore, QtGui
import osso
import imagebrowser, imageeditor, imageprocessing, taskmonitor
from window import Window
import os

class Application(QtGui.QApplication):
    def __init__(self, argv):
        QtGui.QApplication.__init__(self, argv)
        QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.BlankCursor))

        self.mode = None

        # Set up OSSO context.
        self._osso_context = osso.Context('AugIM', '2.0.0', False)
        osso_rpc = osso.Rpc(self._osso_context)
        osso_rpc.set_rpc_callback('edu.daimi.augim',\
                                  'edu/daimi/augim',\
                                  'edu.daimi.augim',\
                                  self.cb_osso,\
                                  self._osso_context)

        # Set up a threadpool for doing heavy work.
        self._threadpool = imageprocessing.ThreadPool(self)

        # Load the images.
        self.imageloader = imagebrowser.ImageLoader(self.get_image_path(),
                                                    self.get_display_size())
        try:
            message = self.imageloader.scan()
            self.popup_message(message[1])
        except Exception, e:
            self.popup_message("Error reading images from %s"%self.get_image_path())
            raise e

        # Create the model for the task monitor and hook it up to the threadpool.
        self._monitorModel = taskmonitor.TableModel()
        self._threadpool.taskStarted.connect(self._monitorModel.taskStarted)
        self._threadpool.taskProgress.connect(self._monitorModel.taskProgress)
        self._threadpool.taskFinished.connect(self._monitorModel.taskFinished)

        # Create main window.
        self.window = Window(self)
        
        # Set the initial mode.
        self.setMode('Browser')
        
        # Display the main window.
        self.window.show()

    def setMode(self, mode):
        mode = str(mode)
        if mode != self.mode:
            self.mode = mode

            # Initialize the new mode.
            if self.mode == 'Browser':
                browser = imagebrowser.GraphicsView(self.imageloader, self.window)
                browser.toggleFullScreen.connect(self.window.toggleFullScreen)
                browser.modeChange.connect(self.setMode)
                self.window.setCentralWidget(browser)
                
            elif self.mode == 'Editor':
                editor = imageeditor.GraphicsView(self.window)
                editor.toggleFullScreen.connect(self.window.toggleFullScreen)
                editor.modeChange.connect(self.setMode)
                editor.applyImageOperations.connect(self._threadpool.performTask)
                editor.setImage(self.imageloader.current_multi_image())
                self.window.setCentralWidget(editor)
                
            elif self.mode == 'Monitor':
                monitor = taskmonitor.MainWidget(self._monitorModel)
                monitor.modeChange.connect(self.setMode)                
                self.window.setCentralWidget(monitor)
                
            self.window.setWindowTitle('AugIm - %s'%self.mode)

    def cb_osso (self, interface, method, arguments, user_data):
        print 'received an OSSO RPC.'

    def popup_message(self, message):
        note = osso.SystemNote(self._osso_context)
        note.system_note_infoprint(message)

    def get_image_path(self):
        return os.environ['HOME'] + '/MyDocs/DCIM'
    
    def get_display_size(self):
        return (800, 480)

    def run(self):
        return self.exec_()
        
